#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time ： 2023/12/27 12:03
@Auth ： chenglu
@File ：app1.py
@IDE ：PyCharm
@Motto：ABC(Always Be Coding)
"""
from flask import Flask, render_template
from pyecharts.charts import Bar
from pyecharts.charts import Pie
from pyecharts import options as opts
import  pymysql,time

app = Flask(__name__)

def myConnect():
    conn = pymysql.connect(
        host = "123.56.187.176",
        port=1101,
        user = "niit",
        passwd="123456",
        charset="utf8",
        database="huel"
    )
    return  conn

def query():
    cursor = myConnect().cursor()
    sql = "SELECT NAME ,SUM(COUNT) AS num FROM teacher GROUP BY NAME;"
    cursor.execute(sql)
    myConnect().commit()
    data = cursor.fetchall()

    cursor.close()
    myConnect().close()
    return  data

@app.route('/')
def index():
    data = query()
    #  名字
    name =[]
    #  数量
    num = []
    for row in data:
       #  名字
       name.append( row[0] )
       #  数量
       num.append( row[1])
       print("名字:"+str(name) + "数量:"+str( num ))

    # 饼状图
    pie = (
        Pie()
        .add("", [ list(z) for z in zip( name, num  ) ])
        .set_global_opts(title_opts=opts.TitleOpts(title="Pie Chart"))
    )
    return render_template('second_page.html', pie=pie.render_embed())


if __name__ == '__main__':
    app.run(debug=True)
